package com.schedule;

import com.dao.AppointDAO;
import com.dao.CollectRecordDAO;
import com.dao.SignUpDAO;
import com.model._do.AppointmentDO;
import com.model._do.CollectResultDO;
import com.model._do.SignUpDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Configuration
@EnableScheduling
public class AppointSchedule {

    @Autowired
    private SignUpDAO signUpDAO;

    @Autowired
    private AppointDAO appointDAO;

    @Autowired
    private CollectRecordDAO collectRecordDAO;

    Logger logger = Logger.getLogger("AppointSchedule");

    @Scheduled(cron = "0 */10 * * * *")
    private void flushAppoint(){
        List<AppointmentDO> appList = appointDAO.getUnConfirm();
        logger.info(String.format("this time flush size:%d",appList.size()));
        appList.forEach(a -> {
            if(Calendar.getInstance().getTime().compareTo(a.getAppointmentTime()) > 0){
                a.setEdAgreement("0");
                appointDAO.flushAppoint(a);
            }
        });
    }

    @Scheduled(cron = "0 */10 * * * *")
    private void flushSign(){
        logger.info("step in sign flush");
        List<SignUpDO> signList = signUpDAO.getUnCon();
        signList.forEach(s -> {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 7);
            if(calendar.getTime().compareTo(s.getGmtCreate())>0){
                if(StringUtils.isEmpty(s.getDoctorConfirm())){
                    s.setDoctorConfirm("disagree");
                }else{
                    s.setPatientConfirm("disagree");
                }
            }
        });
    }

    @Scheduled(cron = "0 */10 * * * *")
    private void flushCollect(){
        logger.info("step in collect flush");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date date = calendar.getTime();
        collectRecordDAO.dropTimeOver(date);
    }

}
